<?php
/**
 * VerifyCodesController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-16 03:47:08
 * @modified    2023-02-16 03:47:08
 *
 */

namespace App\Http\Controllers;

use App\Services\VerifyCodeService;
use Illuminate\Http\Request;

class VerifyCodesController extends BaseController
{
    /**
     * @OA\Post(
     *     path="/verify_code",
     *     operationId="verify_code_post",
     *     tags={"VerifyCode"},
     *     summary="生成手机验证码",
     *     description="生成手机验证码",
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"account"},
     *                 @OA\Property(property="account", description="用户电话号码", type="string")
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index(Request $request)
    {
        try {
            (new VerifyCodeService($request->get('account')))->generate();
            return message('验证码已发送，请注意查收', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/verify_code_pwd",
     *     operationId="verify_code_pwd",
     *     tags={"VerifyCode"},
     *     summary="生成手机验证码(重置密码)",
     *     description="生成手机验证码(重置密码使用)",
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"mobile"},
     *                 @OA\Property(property="mobile", description="电话号码", type="string")
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function resetPassword()
    {
        try {
            (new VerifyCodeService(\request()->get('mobile')))->forResetPwd();
            return message('验证码已发送，请注意查收', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }
}